package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.entity.JxLaboratoryEntity;
import io.renren.modules.sys.service.JxLaboratoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.JxComputyDeviceEntity;
import io.renren.modules.sys.service.JxComputyDeviceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 电脑设备
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-10-08 18:05:43
 */
@RestController
@RequestMapping("sys/jxcomputydevice")
public class JxComputyDeviceController {
    @Autowired
    private JxComputyDeviceService jxComputyDeviceService;

    @Autowired
    private JxLaboratoryService jxLaboratoryService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:jxcomputydevice:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jxComputyDeviceService.getAllWithPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:jxcomputydevice:info")
    public R info(@PathVariable("id") Long id){
        JxComputyDeviceEntity jxComputyDevice = jxComputyDeviceService.getById(id);

        return R.ok().put("jxComputyDevice", jxComputyDevice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:jxcomputydevice:save")
    public R save(@RequestBody JxComputyDeviceEntity jxComputyDevice){
        ValidatorUtils.validateEntity(jxComputyDevice,AddGroup.class);
        jxComputyDeviceService.save(jxComputyDevice);
        totalDevice(jxComputyDevice.getLaboratoryId());
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:jxcomputydevice:update")
    public R update(@RequestBody JxComputyDeviceEntity jxComputyDevice){
        ValidatorUtils.validateEntity(jxComputyDevice ,UpdateGroup.class);
        jxComputyDeviceService.updateById(jxComputyDevice);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:jxcomputydevice:delete")
    public R delete(@RequestBody Long[] ids){
        JxComputyDeviceEntity entity=jxComputyDeviceService.getById(ids[0]);
        jxComputyDeviceService.removeByIds(Arrays.asList(ids));
        totalDevice(entity.getLaboratoryId());
        return R.ok();
    }


    /**
     * 所有的设备
     */
    @RequestMapping("allDevice")
    public R allDevices(@RequestParam Map<String, Object> params){
        List<JxComputyDeviceEntity>  list=jxComputyDeviceService.getAllDevice(params);
        return R.ok().put("devices",list);
    }


    /**
     * 统计电脑设备数量
     */
    public void  totalDevice(Long laboratoryId){
        if(laboratoryId!=null){
            Integer count=jxComputyDeviceService.totalDevice(laboratoryId);
            JxLaboratoryEntity entity=jxLaboratoryService.getById(laboratoryId);
            entity.setDeviceSize(count);
            jxLaboratoryService.saveOrUpdate(entity);

        }
    }

    /**
     * 得到 所有为被选择的设备
     */
    @RequestMapping("/sendfault/{id}")
   public R sendfault(@PathVariable("id") Long id){
        JxComputyDeviceEntity jxComputyDevice = jxComputyDeviceService.getById(id);
        if (jxComputyDevice.getUseStatus().equals("2")){
           jxComputyDevice.setUseStatus("0");
        }else{
            jxComputyDevice.setUseStatus("2");
        }
        jxComputyDeviceService.updateById(jxComputyDevice);
        return R.ok();
   }
}
